package com.imooc.miaosha.service;

import com.imooc.miaosha.dao.GoodsDao;
import com.imooc.miaosha.pojo.domain.Goods;
import com.imooc.miaosha.pojo.domain.OrderInfo;
import com.imooc.miaosha.pojo.vo.GoodsVo;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

@Service
public class GoodsService {
    @Autowired
    GoodsDao goodsDao;
    public List<GoodsVo> listGoodsVo(){
        return goodsDao.listGoodsVo();
    }
    public GoodsVo findGoodsVoById(long id){
        GoodsVo goodsVo = goodsDao.findGoodsVoById(id);
        //判断秒杀状态
        long startAt = goodsVo.getStartDate().getTime();
        long endAt = goodsVo.getEndDate().getTime();
        long now = System.currentTimeMillis();

        int miaoshaStatus=0;//秒杀状态
        long remainSeconds=0;
        if (now<startAt){//秒杀还没开始，倒计时
            remainSeconds=(startAt-now)/1000;//默认是毫秒，转换为秒
        }else if (now>endAt){//秒杀已经结束
            miaoshaStatus=2;
            remainSeconds=-1;
        }else {//秒杀进行中
            miaoshaStatus=1;
        }
        goodsVo.setMiaoshaStatus(miaoshaStatus);
        goodsVo.setRemainSeconds(remainSeconds);
        return goodsVo;
    }


    public void reduceStock(GoodsVo goodsVo) {
        Integer[] a = new Integer[10];
        Arrays.sort(a, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return 0;
            }
        });
        goodsDao.reduceStock(goodsVo.getId());

    }
}
